package com.wlq.demo.entity;

import cn.hutool.core.util.StrUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 操作消息提醒
 * 
 * @author ruoyi
 */
public class AjaxResult extends HashMap<String, Object>
{
    private static final long serialVersionUID = 1L;

    /** 状态码 */
    public static final String CODE_TAG = "code";

    /** 返回内容 */
    public static final String MSG_TAG = "msg";

    /** 返回内容 */
    public static final String MSGEN_TAG = "msgEn";

    /** 数据对象 */
    public static final String DATA_TAG = "data";

    public static Map<String, String> msgEnMap;


    /**
     * 初始化一个新创建的 AjaxResult 对象，使其表示一个空消息。
     */
    public AjaxResult()
    {
    }

    /**
     * 初始化一个新创建的 AjaxResult 对象
     * 
     * @param code 状态码
     * @param msg 返回内容
     */
    public AjaxResult(int code, String msg)
    {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
    }

    /**
     * 初始化一个新创建的 AjaxResult 对象
     * 
     * @param code 状态码
     * @param msg 返回内容
     * @param data 数据对象
     */
    public AjaxResult(int code, String msg, Object data)
    {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        if(StrUtil.isNotBlank(msg)){
            setMsgEn(msg);
        }
        if (null != (data))
        {
            super.put(DATA_TAG, data);
        }
    }

    public void setMsgEn(String msg){
        if(msgEnMap == null){
            msgEnMap = new HashMap<>();
            msgEnMap.put("邮件发送成功","Email sent successfully");
            msgEnMap.put("邮件发送失败","Email sending failed");
            msgEnMap.put("您已收藏此网站","You have bookmarked this website");
            msgEnMap.put("收藏成功","Collection successful");
            msgEnMap.put("取消收藏成功","Cancel Collection Successfully");
            msgEnMap.put("操作成功","Operation succeeded");
            msgEnMap.put("获取成功","Success");
            msgEnMap.put("文件已存在","File already exist");
            msgEnMap.put("文件上传成功","File uploaded successfully");
            msgEnMap.put("操作失败","Operation failed");
            msgEnMap.put("发送短信失败","Failed to send SMS");
            msgEnMap.put("未登录用户","User not logged in");
            msgEnMap.put("文件不存在","File does not exist");
            msgEnMap.put("文件内容不能为空！","File content cannot be empty");
            msgEnMap.put("上传失败","Upload failed");
            msgEnMap.put("没有访问权限，请联系管理员授权","No access rights, please contact the administrator for authorization");
            msgEnMap.put("验证码不能为空","Verification code cannot be empty");
            msgEnMap.put("验证码已失效","The verification code has expired");
            msgEnMap.put("验证码错误","Verification code error");
            msgEnMap.put("验证成功","Verification successful");
            msgEnMap.put("手机号未作更改，请检查","The phone number has not been changed, please check");
            msgEnMap.put("该手机号已存在","The phone number already exists");
            msgEnMap.put("用户/密码必须填写","User/password must be filled in");
            msgEnMap.put("用户密码不在指定范围","The user password is not in the specified range");
            msgEnMap.put("用户名不在指定范围","User name is not in the specified range");
            msgEnMap.put("登录用户不存在","Login user does not exist");
            msgEnMap.put("对不起,您的账号已被删除","Sorry, your account has been deleted");
            msgEnMap.put("对不起,您的账号已停用","Sorry, your account has been deactivated");
            msgEnMap.put("用户手机号必须填写","User mobile phone number must be filled in");
            msgEnMap.put("用户/密码/手机号必须填写","User/password/mobile number must be filled in");
            msgEnMap.put("账户长度必须在2到20个字符之间","Account length must be between 2 and 20 characters");
            msgEnMap.put("密码长度必须在5到20个字符之间","Password length must be between 5 and 20 characters");
            msgEnMap.put("手机号码长度不能超过11个字符","The length of mobile phone number cannot exceed 11 characters");
            msgEnMap.put("用户不存在/密码错误","User does not exist/password error");

            msgEnMap.put("当前系统没有开启注册功能","The registration function is not enabled in the current system");
            msgEnMap.put("保存用户失败，注册账号已存在","Failed to save the user. The registered account already exists");
            msgEnMap.put("新增手机号失败，手机号码已存在","Failed to add mobile phone number, mobile phone number already exists");
            msgEnMap.put("电话号码格式不正确","The phone number format is incorrect");
            msgEnMap.put("新增用户失败，登录账号已存在","Failed to add a user. The login account already exists");
            msgEnMap.put("新增用户失败，手机号码已存在","Failed to add a user. The mobile number already exists");
            msgEnMap.put("新增用户失败，邮箱账号已存在","Failed to add a user. The email account already exists");
            msgEnMap.put("修改用户失败，登录账号已存在","Failed to modify a user. The login account already exists");
            msgEnMap.put("修改用户失败，手机号码已存在","Failed to modify a user. The mobile number already exists");
            msgEnMap.put("修改用户失败，邮箱账号已存在","Failed to modify a user. The email account already exists");
            msgEnMap.put("当前用户不能删除","Current user cannot be deleted");
        }
        if(msgEnMap.containsKey(msg)){
            super.put(MSGEN_TAG, msgEnMap.get(msg));
        }else{
            super.put(MSGEN_TAG, "");
        }

    }

    /**
     * 返回成功消息
     * 
     * @return 成功消息
     */
    public static AjaxResult success()
    {
        return AjaxResult.success("操作成功");
    }

    /**
     * 返回成功数据
     * 
     * @return 成功消息
     */
    public static AjaxResult success(Object data)
    {
        return AjaxResult.success("操作成功", data);
    }

    /**
     * 返回成功消息
     * 
     * @param msg 返回内容
     * @return 成功消息
     */
    public static AjaxResult success(String msg)
    {
        return AjaxResult.success(msg, null);
    }

    /**
     * 返回成功消息
     * 
     * @param msg 返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static AjaxResult success(String msg, Object data)
    {
        return new AjaxResult(200, msg, data);
    }

    /**
     * 返回警告消息
     *
     * @param msg 返回内容
     * @return 警告消息
     */
    public static AjaxResult warn(String msg)
    {
        return AjaxResult.warn(msg, null);
    }

    /**
     * 返回警告消息
     *
     * @param msg 返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static AjaxResult warn(String msg, Object data)
    {
        return new AjaxResult(500, msg, data);
    }

    /**
     * 返回错误消息
     * 
     * @return
     */
    public static AjaxResult error()
    {
        return AjaxResult.error("操作失败");
    }

    /**
     * 返回错误消息
     * 
     * @param msg 返回内容
     * @return 警告消息
     */
    public static AjaxResult error(String msg)
    {
        return AjaxResult.error(msg, null);
    }

    /**
     * 返回错误消息
     * 
     * @param msg 返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static AjaxResult error(String msg, Object data)
    {
        return new AjaxResult(500, msg, data);
    }

    /**
     * 返回错误消息
     * 
     * @param code 状态码
     * @param msg 返回内容
     * @return 警告消息
     */
    public static AjaxResult error(int code, String msg)
    {
        return new AjaxResult(code, msg, null);
    }

    /**
     * 是否为成功消息
     *
     * @return 结果
     */
    public boolean isSuccess()
    {
        return Objects.equals(200, this.get(CODE_TAG));
    }

    /**
     * 是否为错误消息
     *
     * @return 结果
     */
    public boolean isError()
    {
        return !isSuccess();
    }

    /**
     * 方便链式调用
     *
     * @param key
     * @param value
     * @return
     */
    @Override
    public AjaxResult put(String key, Object value)
    {
        super.put(key, value);
        return this;
    }


}
